#!/bin/bash

project_file=$1
composer_update=$2
if [[ $project_file = '' ]]; then
	echo -e "\033[31m 请选择项目!!! \033[0m"
    exit
fi


echo '******批量更新代码脚本开始:******:'

source $project_file

#统一部署服务器
for ((i=0; i<${#unify_server_ips[@]}; i++)); do
	username=`echo ${unify_server_ips[$i]} | cut -d " " -f1`
	ip=`echo ${unify_server_ips[$i]} | cut -d " " -f2`

	if [[ $composer_update = 'composer_update' ]]; then
		command=$unify_server_composer_update_command
	else
		command=$unify_server_pull_command
	fi

	ssh $username@$ip $command

	if [ $? == 0 ]; then
		echo "$username@$ip 执行结果: 执行成功!"
	else
		echo -e "\033[31m $username@$ip 执行结果: 执行失败! \033[0m"
	fi

	echo "执行命令:ssh $username@$ip $command"

done


#特殊部署服务器
for ((i=0; i<${#special_server_ips[@]}; i++)); do
	username=`echo ${special_server_ip[$i]} | cut -d " " -f1`
	ip=`echo ${special_server_ip[$i]} | cut -d " " -f2`

	if [[ $composer_update = 'composer_update' ]]; then
		command=${special_server_composer_update_command[$]}
	else
		command=${special_server_pull_command[$i]}
	fi

	ssh $username@$ip $command

	if [ $? == 0 ]; then
		echo "$username@$ip 执行结果: 执行成功!"
	else
		echo -e "\033[31m $username@$ip 执行结果: 执行失败! \033[0m"
	fi

	echo "执行命令:ssh $username@$ip $command"
	
done



echo -e "\033[32m ******批量更新代码脚本结束****** \033[0m"